package org.bim.view.drag.design.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.bim.view.drag.design.api.dto.UserShareDTO;
import org.bim.view.drag.design.api.vo.ShareUserVisualVO;
import org.bim.view.drag.design.api.vo.TUserVO;
import org.bim.view.drag.design.dao.UserShareMapper;
import org.bim.view.drag.design.dao.VisualMapper;
import org.bim.view.drag.design.entity.TUserShare;
import org.bim.view.drag.design.entity.TVisual;
import org.bim.view.drag.design.exception.BizCommonException;
import org.bim.view.drag.design.model.HttpResult;
import org.bim.view.drag.design.util.AssertUtil;
import org.bim.view.drag.design.util.ThreadLocalUtil;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Slf4j
@Api
@RestController
@RequestMapping("visual/share")
public class VisualShareController {
    @Resource
    private VisualMapper visualMapper;
    @Resource
    private UserShareMapper userShareMapper;
    @GetMapping("list-users")
    public HttpResult<List<ShareUserVisualVO>> listByUser(@ApiParam(required = true, value = "类型，0已分享，1被分享") @RequestParam Integer type) {
        TUserVO userVO = ThreadLocalUtil.getCurrentUser();
        String userId = userVO.getId();
        List<ShareUserVisualVO> list = visualMapper.listByUserShare(userId, type);
        return HttpResult.success(list);
    }
    @PostMapping("/to-user")
    public HttpResult share2Users(@Valid @RequestBody UserShareDTO shareDTO){
        TUserVO userVO = ThreadLocalUtil.getCurrentUser();
        TVisual visual = visualMapper.selectByPrimaryKey(shareDTO.getVisualId());
        AssertUtil.isTrue(visual!=null,new BizCommonException("大屏不存在"));
        shareDTO.getUserIds().forEach(userId->{
            Example example =new Example(TUserShare.class);
            example.createCriteria()
                    .andEqualTo("visualId",visual.getId())
                    .andEqualTo("sourceId",userVO.getId())
                    .andEqualTo("shareId", userId);
            TUserShare userShare =  userShareMapper.selectOneByExample(example);
            if(userShare==null){
                userShare =new TUserShare();
                userShare.setVisualId(visual.getId());
                userShare.setSourceId(userVO.getId());
                userShare.setShareId(userId);
                userShare.setShareRole(shareDTO.getRole());
                userShareMapper.insertSelective(userShare);
            }else {
                userShare.setShareRole(shareDTO.getRole());
                userShareMapper.updateByPrimaryKeySelective(userShare);
            }
        });
        return HttpResult.success();
    }
    @PostMapping("/remove")
    public HttpResult removeShare(@Valid @RequestBody ShareUserVisualVO shareDTO){
        TVisual visual = visualMapper.selectByPrimaryKey(shareDTO.getVisualId());
        AssertUtil.isTrue(visual!=null,new BizCommonException("大屏不存在"));
        userShareMapper.deleteByPrimaryKey(shareDTO.getShareId());
        return HttpResult.success();
    }

}
